# -*- coding: utf-8 -*-
import json

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import DeskTop, ControlCenter


class PreTest_RK3568_Basic_Systemui_0600(TestCase, DeskTop):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 进入桌面，右上角下划进入控制中心，滑动亮度滚动条
    # 预期结果：
    # 滑动滚动条流畅，随实际情况变化，与设置的亮度条同样变化

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)
        CommonOH.setScreenBrightnessValue(self.Phone1, 1)

    def process(self):
        Step('步骤1、进入桌面，右上角下划进入控制中心，滑动亮度滚动条')
        CommonOH.swipe(self.Phone1, **ControlCenter.pull_down)
        CommonOH.wait(self.Phone1, 2)
        slider = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Slider'))
        bounds = slider.get('bounds')
        y = (bounds.get('topY') + bounds.get('bottomY')) // 2
        left_x = bounds.get('leftX')
        right_x = bounds.get('rightX')
        brightness0 = eval(CommonOH.getScreenBrightnessValue(self.Phone1)).get('value')
        CommonOH.swipe(self.Phone1, x1=left_x, y1=y, x2=right_x, y2=y)
        CommonOH.wait(self.Phone1, 1)
        Step('预期结果1、滑动滚动条流畅，随实际情况变化，与设置的亮度条同样变化')
        brightness1 = eval(CommonOH.getScreenBrightnessValue(self.Phone1)).get('value')
        Asserts.assert_greater(brightness1, brightness0)
        CommonOH.swipe(self.Phone1, x1=right_x, y1=y, x2=(left_x + right_x) // 2, y2=y)
        CommonOH.wait(self.Phone1, 1)
        brightness2 = eval(CommonOH.getScreenBrightnessValue(self.Phone1)).get('value')
        Asserts.assert_greater(brightness1, brightness2)

    def teardown(self):
        common_teardown(self.Phone1)
